package com.sunsharing.flow.model;

import com.sunsharing.flow.event.WorkFlowEvent;
import com.sunsharing.flow.eventman.EventManager;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-5-4
 * Time: 下午1:52
 * To change this template use File | Settings | File Templates.
 */
public abstract class GraphElement implements Serializable {

    /**主键**/
    protected String id;

    /**名称**/
    protected String name = null;

    /**描述**/
    protected String desc;

    /**客户端ID*/
    protected String clientId = null;
    /**过程定义**/
    protected ProcessDefinition processDefinition = null;

    public GraphElement() {
    }


    /**
     * 功能描述:
     *    返回的所有支持事件类型
     * @return String[]
     */
    public abstract Class[] getSupportedEventTypes();



    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void setClientId(String clientId)
    {
        this.clientId=clientId;
    }
    public String getClientId()
    {
        return this.clientId;
    }
    public void setProcessDefinition(ProcessDefinition def) {
        this.processDefinition = def;
    }

    public ProcessDefinition getProcessDefinition() {
        return this.processDefinition;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }





    /**
     * 功能描述:
     *     传播事件
     * @param event WorkFlowEvent
     */
    public void multicastEvent(WorkFlowEvent event) {

        //判断传播的事件是否为支持的事件,支持则传播
        if(isSupportedEvent(event)) {
            EventManager.getInstance().multicastEvent(event);
        }
        else {
            throw new IllegalArgumentException("传入的事件:" +
                    event.getClass().getName() + ",该工作流元素不支持传入的事件!" );
        }
    }

    /**
     * 功能描述:
     *     判断是否支持的事件
     * @param event WorkFlowEvent
     * @return boolean
     */
    protected boolean isSupportedEvent(WorkFlowEvent event) {
//        System.out.println("传入的事件类型:" +event.getClass().getName());

//        //获得支持的事件类型
        Class[] eventTypes = getSupportedEventTypes();
//        for (int i=0; i < eventTypes.length; i++) {
//           System.out.println("支持的事件类型:" + eventTypes[i].getName());
//        }

        for (int i=0; i < eventTypes.length; i++) {

            // System.out.println("事件:" + eventTypes[i].getName());
            if(event.getClass().getName().equals(eventTypes[i].getName())) {
                return true;
            }
        }

        return false;
    }



    /**
     * 功能描述:
     *     覆盖equals方法
     * @param obj Object
     * @return boolean
     */
//    public boolean equals(Object obj) {
//
//        //如果为null,返回false
//        if(obj == null) {
//            return false;
//        }
//
//        //如果是同一类型,且唯一标识相同,则返回true
//        if(obj instanceof GraphElement) {
//            GraphElement newObj = (GraphElement)obj;
//            //如果都为新创建的对象,则比较内存地址
//            if (newObj.getId() ==0 && this.getId() == 0) {
//                return newObj==this;
//            }
//            //如果不为新创建的对象,则比较id
//            if(newObj.getId() == this.getId()) {
//                return true;
//            }
//        }
//        return false;
//
//    }
}
